package com.kll.tv.views

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.kll.carolinehmibiz.OperationManager
import com.kll.carolinehmibiz.model.AccountModel
import com.kll.carolinehmibiz.model.OperateListener
import com.kll.carolinehmibiz.net.bean.RefreshTokenResponse
import com.kll.carolinehmibiz.presentation.BasePresenter
import com.kll.carolinehmibiz.presentation.listener.MainContactServiceProtocol
import com.kll.carolinehmibiz.presentation.presenter.MainContactServicePresenter
import com.kll.tv.base.views.BaseActivity
import com.kll.tv.databinding.ActivityFarDiagnosisBinding
import com.kll.tv.utils.AppToast
import com.kll.tv.utils.TUICallingService
import com.tencent.liteav.trtccalling.model.TUICalling
import com.tencent.liteav.trtccalling.model.impl.TUICallingManager
import com.tencent.liteav.trtccalling.model.util.TUICallingConstants
import com.tencent.qcloud.tuicore.util.ToastUtil

/**
 * 远程问诊页
 */
class FarDiagnosisActivity: BaseActivity<BasePresenter>(), MainContactServiceProtocol.IView {
    private lateinit var binding: ActivityFarDiagnosisBinding
    var mCallName: String? = null
    var mCallType: TUICalling.Type?= null
    private var mainContactServicePresenter: MainContactServicePresenter? = null

    override fun initViewBinding(): View {
        binding = ActivityFarDiagnosisBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun createPresenter(): BasePresenter {
        return BasePresenter(this)
    }

    override fun init() {
        TUICallingService.getInstance(this).login()
        mainContactServicePresenter = MainContactServicePresenter(this, this)
        initListener()
    }

    private fun initListener() {
        binding.ivBack.setOnClickListener { finish() }
        binding.ivBackCircular.setOnClickListener { startActivity(Intent(this, MainActivity::class.java))
            finish() }

        binding.ivVoice.setOnClickListener {
            mCallName = TUICallingConstants.TYPE_AUDIO
            mCallType =   TUICalling.Type.AUDIO
            mainContactServicePresenter!!.getAssignCustomerService()
        }

        binding.ivVideo.setOnClickListener {
            mCallName = TUICallingConstants.TYPE_VIDEO
            mCallType =   TUICalling.Type.VIDEO
            mainContactServicePresenter!!.getAssignCustomerService()
        }

        binding.ivVoice.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                binding.tvVoice.setTextColor(Color.parseColor("#008399"))
            } else {
                binding.tvVoice.setTextColor(Color.parseColor("#B5B5B6"))
            }
        }

        binding.ivVideo.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                binding.tvVideo.setTextColor(Color.parseColor("#008399"))
            } else {
                binding.tvVideo.setTextColor(Color.parseColor("#B5B5B6"))
            }
        }
    }


    override fun getRoomIdSuccess(tencentUserId: String, roomid: String) {
        if(TUICallingService.getInstance(this).isLogin()) {
            val userIDs = arrayOf(tencentUserId)
            val bundle = Bundle()
            bundle.putString(TUICallingConstants.PARAM_NAME_TYPE, mCallName)
            bundle.putStringArray(TUICallingConstants.PARAM_NAME_USERIDS, userIDs)
            bundle.putString(TUICallingConstants.PARAM_NAME_GROUPID, "")
            TUICallingManager.sharedInstance().call(userIDs, roomid,mCallType)
        } else {
            ToastUtil.toastLongMessage("分配客服失败，请重试！")
        }
    }

    override fun getRoomIdFailed(code: Int, message: String) {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show()
        if (code == 102) {
            // token过期 刷新token
            OperationManager.getInstancce().refreshToken(object : OperateListener {
                override fun onSuccess(state: Int, obj: Any?) {
                    var data = obj as RefreshTokenResponse
                    AccountModel.getInstance(baseContext).updateUserToken(data.token)
                    mainContactServicePresenter!!.getAssignCustomerService()
                }

                override fun onFail(code: Int, message: String) {
                    AppToast.show(message, 2000)
                    if (code == 101) {
                        loginAgain()
                    }
                }

            })
        }
        if (code == 101) {
            loginAgain()
        }
    }

    /**
     * 分配客服成功
     */
    override fun assignCSSuccessed(accountNo: String) {
        mainContactServicePresenter!!.getRoomId(accountNo)
    }

    /**
     * 分配客服加载中
     */
    override fun showAssignCSLoading() {
        Toast.makeText(this, "获取中...", Toast.LENGTH_SHORT).show()
    }

    /**
     * 分配客服失败
     */
    override fun assignCSFailed(code: Int, message: String) {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show()
        if (code == 102) {
            // token过期 刷新token
            OperationManager.getInstancce().refreshToken(object : OperateListener {
                override fun onSuccess(state: Int, obj: Any?) {
                    var data = obj as RefreshTokenResponse
                    AccountModel.getInstance(baseContext).updateUserToken(data.token)
                    mainContactServicePresenter!!.getAssignCustomerService()
                }

                override fun onFail(code: Int, message: String) {
                    AppToast.show(message, 2000)
                    if (code == 101) {
                        loginAgain()
                    }
                }

            })
        }
        if (code == 101) {
            loginAgain()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        // 销毁presenter
        mainContactServicePresenter!!.onDismiss()
    }

}